import { get, post, put, del } from '@/utils/request';
import type { Product, ProductListParams, PaginationResponse, ApiResponse } from '@/types/api';

/**
 * 商品相关API
 */
export class ProductAPI {
  /**
   * 获取商品列表
   * @param params 查询参数
   * @returns Promise<PaginationResponse<Product>>
   */
  static async getProductList(params: ProductListParams) {
    const response = await get<PaginationResponse<Product>>('/products', params);
    return response.data;
  }

  /**
   * 获取商品详情
   * @param productId 商品ID
   * @returns Promise<Product>
   */
  static async getProductDetail(productId: number): Promise<Product> {
    const response = await get<Product>(`/products/${productId}`);
    return response.data;
  }

  /**
   * 创建商品
   * @param productData 商品数据
   * @returns Promise<Product>
   */
  static async createProduct(
    productData: Omit<Product, 'id' | 'createdAt' | 'updatedAt'>
  ): Promise<Product> {
    const response = await post<Product>('/products', productData);
    return response.data;
  }

  /**
   * 更新商品
   * @param productId 商品ID
   * @param productData 商品数据
   * @returns Promise<Product>
   */
  static async updateProduct(productId: number, productData: Partial<Product>): Promise<Product> {
    const response = await put<Product>(`/products/${productId}`, productData);
    return response.data;
  }

  /**
   * 删除商品
   * @param productId 商品ID
   * @returns Promise<void>
   */
  static async deleteProduct(productId: number): Promise<void> {
    await del(`/products/${productId}`);
  }

  /**
   * 搜索商品
   * @param keyword 搜索关键词
   * @param params 其他参数
   * @returns Promise<PaginationResponse<Product>>
   */
  static async searchProducts(keyword: string, params?: Omit<ProductListParams, 'keyword'>) {
    const response = await get<PaginationResponse<Product>>('/products/search', {
      keyword,
      ...params,
    });
    return response.data;
  }
}
